package com.groupsix.controller;

import com.groupsix.pojo.SysUser;
import com.groupsix.service.SysUserService;
import com.groupsix.vo.LoginInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
public class LoginController {

    @Autowired
    private SysUserService userservice;
    //登录
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(@RequestBody LoginInfoVo loginInfoVo, HttpSession session){
        System.out.println("tel = " + loginInfoVo.getTel());
        //根据tel获取用户信息
        System.out.println("password = " + loginInfoVo.getPassword());
        SysUser userInfo = userservice.findUserInfoByTel(loginInfoVo.getTel());
        //如果用户不为空 用户密码相同,将存入id和tel存入session
        if (userInfo!=null && userInfo.getPassword().equals(loginInfoVo.getPassword())){
            session.setAttribute("uid",userInfo.getId());
            session.setAttribute("utel",userInfo.getTel());
            return "success";
        }else {
            return "error";
        }
    }


    //注册
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public String register(@RequestBody SysUser sysUser){
        boolean b = userservice.addUser(sysUser);
        if (b){
            return "success";
        }else {
            return "fail";
        }
    }
}
